package com.own.business.system.menu.business.service.impl;

import com.own.business.system.menu.business.service.SystemUserRoleService;
import com.own.business.system.menu.entity.bo.SystemUserRoleBo;
import com.own.business.system.menu.entity.map.SystemUserRoleMapVo;
import com.own.business.system.menu.entity.po.SystemUserRole;
import com.own.business.system.menu.entity.query.SystemUserRoleQuery;
import com.own.business.system.menu.entity.vo.SystemUserRoleVo;
import com.own.business.system.menu.mapper.SystemUserRoleMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.store.core.util.PermissionCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * SystemUserRoleServiceImpl
 *
 * @author chenxueli
 * @date 2024-03-31 10:00:18
 */
@Slf4j
@Service
public class SystemUserRoleServiceImpl extends AbstractBaseService<
        SystemUserRole,
        SystemUserRoleBo,
        SystemUserRoleVo,
        SystemUserRoleMapVo,
        SystemUserRoleQuery,
        SystemUserRoleMapper
        > implements SystemUserRoleService {

    @Resource
    private SystemUserRoleMapper systemUserRoleMapper;

    @Resource
    private PermissionCacheUtil permissionCacheUtil;

    /**
     * 根据用户id查询角色id列表
     *
     * @param userId 用户id
     * @return 角色id列表
     */
    @Override
    public List<Long> listRoleIdListByUserId(Long userId) {
        var wrapper = lambdaQueryWrapper().eq(SystemUserRole::getFkUserId, userId);
        return listByWrapper(wrapper).stream().map(SystemUserRole::getFkRoleId).toList();
    }

    /**
     * 根据用户id删除用户角色关联
     *
     * @param userId 用户id
     */
    @Override
    public void deleteByUserId(Long userId) {
        var wrapper = lambdaQueryWrapper().eq(SystemUserRole::getFkUserId, userId);
        systemUserRoleMapper.delete(wrapper);
        permissionCacheUtil.setUserRoleTimestamp(userId);
    }

    /**
     * 根据角色id删除
     *
     * @param roleId 角色id
     */
    @Override
    public void deleteByRoleId(Long roleId) {
        var wrapper = lambdaQueryWrapper().eq(SystemUserRole::getFkRoleId, roleId);
        systemUserRoleMapper.delete(wrapper);
        permissionCacheUtil.setGlobalTimestamp();
    }

}
